<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="z" uri="zz" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>文件上传</title>
    <%@include file="/views/common/common.jsp" %>
    <script type="text/javascript">
        $(function () {
            var return_url_head = '${sp.fileserverurl}';
            var upload_url = return_url_head + '/upload?filepath=' + $("#zid").val() + '&return_url_head=' + encodeURIComponent(return_url_head);
            //var upload_url = return_url_head+'/upload?filepath='+$("#zid").val();
            $("#uploadify").uploadifive({
                uploadScript: upload_url,
                auto: true,
                fileDataName: 'fileData',
                fileObjName: 'fileData',
                buttonText: '请选择要上传的文件',
                queueID: 'queue',
                height: 50,
                width: 220,
                multi: false,
                onFallback: function () {
                    $("#alertInfo").text("您的浏览器不支持HTML5文件上传控件，请使用支持HTML5的浏览器后使用。如：Chrome")
                }, onUploadComplete: function (file, data) {
                    var obj = JSON.parse(data);
                    if (obj.code == "SUCCESS") {
                        var ColumnId = $("#ColumnId").val();

                        $("#btn").val = data.path
                        $("#url").attr("value", obj.data)
                        $("#msg").text("上传成功,请发放礼券")

                        $("#btnsend").show()

                        //alertMessagerAutoOff('成功删除'+data.data+'条记录');
                        parent.alertMessagerAutoOff('' + data.msg);

                        if (isNotNull(ColumnId)) {
                            window.opener.OpenFileUploadReturn(ColumnId, obj.data);
                        }
                        //  window.close();
                    } else {
                        $("#alertInfo").text(obj.msg);
                        $("#msg").text("上传失败,请重新上传")
                        $("#btnsend").hide()
                    }

                }, onAddQueueItem: function (file) {
                    /* if(/.*[\u4e00-\u9fa5]+.*$/.test(file.name)) {
                        $("#alertInfo").text("文件名,不能包含中文!")
                        $("#uploadify").uploadifive('cancel',$('.uploadifive-queue-item').first().data('file'));
                        $("#uploadify").uploadifive('clearQueue');
                    } */
                }
            });
        });

        function resolve() {
            var url = $("#url").val()
            if (url != null) {
                $.ajax({
                    type: "get",
                    url: "resolve?url=" + encodeURIComponent(url),
                    async: false,
                    success: function (data) {
                        if(data.code == 'SUCCESS'){
                           // parent.alertMessagerAutoOff("导入成功")
                            window.opener.RefreshList();
                            window.close();
                        }else{
                            $("#msg").text("发放失败,请下载文件查看原因")

                            var href = data.data // 下载链接
                            var a = document.createElement('a');
                            a.setAttribute('href', href);
                            a.setAttribute('target', '_blank');
                            a.setAttribute('id', 'startTelMedicine');
                            a.text = "下载错误文件"
                            if(document.getElementById('startTelMedicine')) {
                                document.body.removeChild(document.getElementById('startTelMedicine'));
                            }
                            document.getElementById("liquan").appendChild(a);
                            //a.onclick(dismiss())
                            document.getElementById("startTelMedicine").onclick = () => {window.close()
                            }
                        }
                    },
                    error: function (data) {
                        window.close();
                    }
                });
            }
        }
    </script>
</head>
<body>
<div class="container-fluid">
    <div class="row-fluid">
        <input type="hidden" id="ColumnId" value="${bean.ColunmId}"/>
        <input type="hidden" id="zid" value="${bean.zid}"/>
        <input id="uploadify" name="fileData" type="file"/>
        <input id="url" type="hidden">
    </div>
    <div class="row-fluid">
        <span id="alertInfo"></span>
        <div id="queue"></div>
        <span id="msg" type="text" style="color: #ff0000;font-size: 10px;float: right"></span>
    </div>
    <div id="liquan">
        <input id="btnsend" type="button" value="礼券发放" onclick="resolve()" style="display:none;margin-top: 20px">
    </div>
<%--    <div>--%>
<%--        <a id="errorUrl" href="#" download = "error.xls"  style="display: none" onclick="dismiss()">下载错误文件</a>--%>
<%--    </div>--%>
</div>
<%@include file="/views/common/body.jsp" %>
</body>
</html>